package com.myideas.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
 
@Entity
public class Knowledge implements BaseObject, Serializable {
	private Long id; 
	private Long version;
	private String summary;
	private String description;
	private User user;
	
	public Knowledge() {
		System.out.println("Instantiating Knowledge object");
	}
	
	@Column(nullable=false)
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
    @ManyToOne(optional=false, fetch=FetchType.EAGER) 
    @JoinColumn(name="user_id", nullable=false, updatable=true)
	public User getUser() {
		return user;
	}
    
	public void setUser(User user) {
		this.user = user;
	}
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	@Version
	public Long getVersion() {
		return version;
	}
	public void setVersion(Long version) {
		this.version = version;
	}
	@Override
	public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Knowledge)) {
            return false;
        }

        final Knowledge k = (Knowledge) o;

        //user and summary are not supposed to be null
        return user.equals(k.user) && summary.equals(k.summary);
//        return !(summary != null ? !summary.equals(k.getSummary()) : k.getSummary() != null);
	}
	@Override
	public String toString() {
		return user.getUsername() + " " + summary;
	}
	
	@Override
	public int hashCode() {
    	return (summary != null ? summary.hashCode() : 0);
	}
}
